﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Omnia.Infrastructure;
using Omnia.Enums;
using Omnia.Library.Helpers;
using System.Windows.Input;
using Omnia.Library;
using System.Windows;
using System.Reflection;
using System.Data.Linq;
using Omnia.DataModels;
using System.IO.IsolatedStorage;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.IO;
using System.ComponentModel;

namespace Omnia.ViewModels
{
    public class ViewModelBase : NotificationObject
    {
        public IDataContext Database;

        public ViewModelBase()
        {
            Database = new IDataContext(ConstKeys.ConnectionString);
            globalSettings.SettingsChanged -= globalSettings_SettingsChanged;
            globalSettings.SettingsChanged += globalSettings_SettingsChanged;
        }

        void globalSettings_SettingsChanged(object sender, EventArgs e)
        {
            RaisePropertyChanged(() => GlobalSettings);
        }

        public virtual void Save()
        {
            ChangeSet changeSet = Database.GetChangeSet();
            Database.SubmitChanges();
            //Type t = typeof(MainViewModel);
            //ConstructorInfo constructor = t.GetConstructor(Type.EmptyTypes);
            //object classObject = constructor.Invoke(new object[] { });
            //MethodInfo method = t.GetMethod("Refresh");
            //method.Invoke(classObject, null);
        }

        public virtual void Refresh()
        {
            Type type = GetType();
            PropertyInfo[] properties = type.GetProperties();

            foreach (PropertyInfo p in properties)
            {
                RaisePropertyChanged(() => p.Name);
            }

        }

        #region CommonCollectionProperties
        public IEnumerable<PaymentWayValue> PaymentWays
        {
            get
            {
                return EnumHelper.GetEnumValues<PaymentWayValue>();
            }
        }

        public IEnumerable<TransactionKindValue> TransactionKinds
        {
            get
            {
                return EnumHelper.GetEnumValues<TransactionKindValue>();
            }
        }

        public IEnumerable<CardKindValue> CardKinds
        {
            get
            {
                return EnumHelper.GetEnumValues<CardKindValue>();
            }
        }
        #endregion

        #region Options
        
        public IEnumerable<string> ExistsLanguageFile
        {
            get
            {
                if (IsDesignMode)
                {
                    yield return "zh-CN";
                }
                else
                {
                    using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        foreach (string file in store.GetFileNames(@"\Languages\*.xaml"))
                        {
                            yield return Path.GetFileNameWithoutExtension(file);
                        }
                    }
                }
            }
        }

        public ImageSource BackgroundImageSource
        {
            get
            {
                BitmapImage image = new BitmapImage();
                if (GlobalSettings.Location != ImageLocationValue.Isolated)
                {
                    try
                    {
                        image.SetSource(Application.GetResourceStream(new Uri(GlobalSettings.BackgroundImage, UriKind.RelativeOrAbsolute)).Stream);
                    }
                    catch
                    {
                        GlobalSettings.BackgroundImage = "/Omnia.Wallet;component/Assets/desert.png";
                        GlobalSettings.Location = ImageLocationValue.Applocation;
                        image.SetSource(Application.GetResourceStream(new Uri(GlobalSettings.BackgroundImage, UriKind.RelativeOrAbsolute)).Stream);
                    }
                }
                else
                {
                    using (Stream stream = IsolatedStorageFile.GetUserStoreForApplication().OpenFile(GlobalSettings.BackgroundImage, FileMode.Open, FileAccess.Read))
                    {
                        image.SetSource(stream);
                    }
                }
                return image;
            }
        }

        private OptionInfo globalSettings = new OptionInfo();

        public OptionInfo GlobalSettings
        {
            get { return globalSettings; }
            set
            {
                globalSettings = value;
                RaisePropertyChanged(() => GlobalSettings);
            }
        }
        #endregion

        public bool IsDesignMode
        {
            get
            {
                return DesignerProperties.IsInDesignTool;//(bool)DependencyPropertyDescriptor.FromProperty(DesignerProperties.IsInDesignModeProperty, typeof(FrameworkElement)).Metadata.DefaultValue;
            }
        }

        #region Command
        public ICommand ResetCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        GlobalSettings.Reset();
                    }
                };
            }
        }
        #endregion
    }
}
